#ifndef _router_H_
#define _router_H_

#include<unordered_map>
#include<set>
#include "./enum.h"
#include<list>
#include<functional>
#include "./client.h"
#include<filesystem>
#include <utility>

namespace Http{
	struct RouterPoint{
		uint16_t id;//  smaller has more weight
		char is_path;
		char is_strict;
	//	uint16_t mode;//set higher 8 bit:file path,   set lower 8 bit: start_with method;
		std::string url;
		std::filesystem::path path;//relative path
		std::function<void(Request*,Reply*)> handler;
	bool operator < (const RouterPoint & v) const
    {
        return id < v.id;
    } ;
		bool operator == (const RouterPoint & v) const
    {
        return id == v.id;
    } ;

	};

class Router {

		std::set<RouterPoint> handlers;
		
		std::string detect_filetype(std::filesystem::path p);
		const RouterPoint* find_handler(const char * buf,int len);
		public:
			Router();

			/**
			 * add handler before running server.
			 * otherwise you have to restart it.
			*/
			bool add_handler(std::string url, std::string path,uint16_t sort=-1,bool is_strict=false);//-1: push back
			bool add_handler(std::string url, std::function<void(Request*,Reply*)> f,uint16_t sort=-1,bool is_strict=false);
			
		//	void handle(Client* c);
			void handle(Request* req,Reply* rep);
	};
}
#endif